//
//  ViewController.m
//  06-nsoperation依赖和监听
//
//  Created by J on 16/2/24.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   
    
    NSOperationQueue * queue = [[NSOperationQueue alloc]init];//默认并发
    
   NSBlockOperation* op1 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"op1:%@",[NSThread currentThread]);
    }];
    NSBlockOperation* op2 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"op2:%@",[NSThread currentThread]);
    }];
    NSBlockOperation* op3 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"op3:%@",[NSThread currentThread]);
    }];
    NSBlockOperation* op4 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"op4:%@",[NSThread currentThread]);
    }];
    
    [op3 setCompletionBlock:^{
        
        NSLog(@"op3执行完毕");
        NSLog(@"%@",[NSThread currentThread]);//在子线程中执行的
    }];
    
 
    [op4 setCompletionBlock:^{
        
        NSLog(@"op4执行完毕");
    }];
 
    //设置依赖要在添加之前   可以跨队列依赖  只要是operation就能添加依赖关系
    
    [op3 addDependency:op1];
    [op3 addDependency:op2];
    [op3 addDependency:op4];


    
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];
    [queue addOperation:op4];

    
}

@end
